﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Subgurim.Controles;
using System.Data;

namespace GlobalTracker.WebSite.AreaRestrita
{
  public partial class CercaEletronica : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        WebServiceReference.GeneralService service = new WebServiceReference.GeneralService();
        service.Url = GlobalTracker.WebSite.Properties.Settings.Default.GlobalTracker_WebSite_WebServiceReference_GeneralService;
        string s = service.TesteConexao();
        DataSet ds;
        
        if (Session["TipoLogin"] == "Administrador")
          ds = service.RecuperarListagem(string.Empty, "SELECT IMEI, NOME FROM DISPOSITIVOMOVEL");
        else
          ds = service.RecuperarListagem(string.Empty, "SELECT IMEI, NOME FROM DISPOSITIVOMOVEL WHERE IMEI = '" + Session["Login"].ToString() + "'");

        if (ds.Tables.Count > 0)
        {
          for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
          {
            ComboBoxDispositivoMovel.Items.Add(ds.Tables[0].Rows[i]["IMEI"].ToString() + " - " + ds.Tables[0].Rows[i]["NOME"].ToString(),
              ds.Tables[0].Rows[i]["IMEI"].ToString());
          }

          if (ds.Tables[0].Rows.Count > 0)
          {
            ComboBoxDispositivoMovel.SelectedIndex = 0;



            string[] arrayCoordenadas = service.RecuperarCercaEletronica(string.Empty, ComboBoxDispositivoMovel.SelectedItem.Value.ToString());

            if (arrayCoordenadas.Length > 0)
            {
              List<GLatLng> pontosOriginal = new List<GLatLng>();
              List<GLatLng> pontos = new List<GLatLng>();
              string ss =string.Empty;
              for (int i = 0; i < arrayCoordenadas.Length; i++)
              {
                string[] latLong = arrayCoordenadas[i].Split(';');
                if (latLong.Length > 1)
                {
                  pontosOriginal.Add(new GLatLng(System.Convert.ToDouble(latLong[0].ToString()), System.Convert.ToDouble(latLong[1].ToString())));
                }
                
              }

              Response.Write(ss);
              if (pontosOriginal.Count >= 3)
              {
                LimparAreaDefinida();
                Session["PontosOriginal"] = pontosOriginal;

                for (int i = 0; i < pontosOriginal.Count; i++)
                {
                  pontos.Add(pontosOriginal[i]);
                }

                GPolygon poligono = new GPolygon(pontos, "557799", 3, 0.5, "237464", 0.5);
                poligono.close();
                GMapAreaMonitoramento.Add(poligono);
              }
              else
              {
                LimparAreaDefinida();
              }
            }
          }
        }

        ds.Dispose();
        ds = null;
        service.Dispose();
        service = null;
      }
    }

    protected string GMapAreaMonitoramento_Click(object s, GAjaxServerEventArgs e)
    {
      if (CheckBoxAtivarMarcacaoArea.Checked)
      {
        List<GLatLng> pontos = new List<GLatLng>();
        List<GLatLng> pontosOriginal;
        if (Session["PontosOriginal"] == null)
        {
          pontosOriginal = new List<GLatLng>();
          pontosOriginal.Add(e.point);
          Session["PontosOriginal"] = pontosOriginal;
        }
        else
        {
          pontosOriginal = (List<GLatLng>)Session["PontosOriginal"];
          pontosOriginal.Add(e.point);
          Session["PontosOriginal"] = pontosOriginal;
        }

        if (pontosOriginal.Count >= 3)
        {
          LimparAreaDefinida();
          Session["PontosOriginal"] = pontosOriginal;

          for (int i = 0; i < pontosOriginal.Count; i++)
          {
            pontos.Add(pontosOriginal[i]);
          }

          GPolygon poligono = new GPolygon(pontos, "557799", 3, 0.5, "237464", 0.5);
          poligono.close();
          GMapAreaMonitoramento.Add(poligono);
        }
      }
      return default(string);
    }

    private void LimparAreaDefinida()
    {
      try
      {
        GMapAreaMonitoramento.resetPolygon();
      }
      catch { }

      try
      {
        GMapAreaMonitoramento.resetControls();
      }
      catch { }

      try
      {
        GMapAreaMonitoramento.resetPolygon();
      }
      catch { }

      Session["PontosOriginal"] = null;
    }

    protected void ButtonLimpar_Click(object sender, EventArgs e)
    {
      LimparAreaDefinida();
    }

    protected void ButtonSalvar_Click(object sender, EventArgs e)
    {
      if (Session["PontosOriginal"] != null)
      {
        List<GLatLng> pontos = new List<GLatLng>();
        pontos = (List<GLatLng>)Session["PontosOriginal"];

        if (pontos.Count > 0)
        {
          string coordenadas = string.Empty;

          for (int i = 0; i < pontos.Count; i++)
          {
            coordenadas += pontos[i].lat.ToString() + ";" + pontos[i].lng.ToString() + "#";
          }

          WebServiceReference.GeneralService service = new WebServiceReference.GeneralService();
          service.Url = GlobalTracker.WebSite.Properties.Settings.Default.GlobalTracker_WebSite_WebServiceReference_GeneralService;
          service.InserirCercaEletronica(string.Empty, ComboBoxDispositivoMovel.SelectedItem.Value.ToString(), true, coordenadas);

        }
      }
    }

    protected void ComboBoxDispositivoMovel_SelectedIndexChanged(object sender, EventArgs e)
    {
      WebServiceReference.GeneralService service = new WebServiceReference.GeneralService();
      service.Url = GlobalTracker.WebSite.Properties.Settings.Default.GlobalTracker_WebSite_WebServiceReference_GeneralService;
      string[] arrayCoordenadas = service.RecuperarCercaEletronica(string.Empty, ComboBoxDispositivoMovel.SelectedItem.Value.ToString());

      if (arrayCoordenadas.Length > 0)
      {
        List<GLatLng> pontosOriginal = new List<GLatLng>();
        List<GLatLng> pontos = new List<GLatLng>();

        for (int i = 0; i < arrayCoordenadas.Length; i++)
        {
          string[] latLong = arrayCoordenadas[i].Split(';');
          if (latLong.Length > 1)
          {
            pontosOriginal.Add(new GLatLng(float.Parse(latLong[0].ToString()), float.Parse(latLong[1].ToString())));
          }
        }        

        if (pontosOriginal.Count >= 3)
        {
          LimparAreaDefinida();
          Session["PontosOriginal"] = pontosOriginal;

          for (int i = 0; i < pontosOriginal.Count; i++)
          {
            pontos.Add(pontosOriginal[i]);
          }

          GPolygon poligono = new GPolygon(pontos, "557799", 3, 0.5, "237464", 0.5);
          poligono.close();
          GMapAreaMonitoramento.Add(poligono);
        }
        else
        {
          LimparAreaDefinida();
        }
      }
    }
  }
}